'''
Created on Jul 20, 2010

@author: behrooz
'''
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext as _
from amscms.payment.managers import UserPaymentPolicyManager, \
    PaymentCreditManager, PaymentCreditHistoryManager,\
    CreditDepositHistoryManager
from amscms.ams.models import AdOrder

class PaymentCredit(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_("Id"))
    user = models.ForeignKey(User, unique=True)
    balance = models.FloatField(default=1000, verbose_name=_("Balance"))
    last_modified = models.DateTimeField(verbose_name=_("Last Modified"), auto_now=True)
    objects = PaymentCreditManager()
    class Meta:
        db_table = "pay_credit"
        verbose_name = _("Payment Credit")
        verbose_name_plural = _("Payment Credits")
        
class PaymentCreditHistory(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_("Id"))
    credit = models.ForeignKey(PaymentCredit)
    amount = models.FloatField(verbose_name=_("Amount"))
    order = models.ForeignKey(AdOrder, unique=True)
    last_modified = models.DateTimeField(verbose_name=_("Last Modified"), auto_now_add=True)
    objects = PaymentCreditHistoryManager()
    class Meta:
        db_table = "pay_credit_history"
        verbose_name = _("Payment Credit History")
        verbose_name_plural = _("Payment Credit Histories")
        
class CreditDepositHistory(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_("Id"))
    credit = models.ForeignKey(PaymentCredit)
    amount = models.FloatField(verbose_name=_("Amount"))
    last_modified = models.DateTimeField(verbose_name=_("Last Modified"), auto_now_add=True)
    objects = CreditDepositHistoryManager()
    class Meta:
        db_table = "pay_dpst_crdt_history"
        verbose_name = _("Payment Deposit History")
        verbose_name_plural = _("Payment Deposit Histories")

class PaymentPolicy(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_("Id"))
    name = models.CharField(max_length=32, verbose_name=_("Name"))
    isfree = models.BooleanField(verbose_name=_("Is Free"), default=False)
    
    def __unicode__(self):
        return self.name 
    
    class Meta:
        db_table = "pay_policy"
        verbose_name = _("Payment Policy")
        verbose_name_plural = _("Payment Policies")
        
class UserPaymentPolicy(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_("Id"))
    user = models.ForeignKey(User)
    policy = models.ForeignKey(PaymentPolicy)
    objects = UserPaymentPolicyManager()
    class Meta:
        db_table = "pay_userpolicy"
        unique_together = (("user", "policy"),)
        verbose_name = _("User Payment Policy")
        verbose_name_plural = _("User Payment Policies")

        
